
def open_file(path):
    try:
        f = open(path, 'r')
        text=f.read()
        # print(text)
    finally:
        if f:
            f.close()
        return text


def read_keywords(path):
    keyword_list=[]
    file = open(path, 'r')
    for line in file.readlines():  
        line=line.strip('\n')
        keyword_list.append(line)
    return keyword_list

def search_keyword(search):
    start = 0
    lists=[]
    while True:
        index = text.find(search, start)
        # if search string not found, find() returns -1
        # search is complete, break out of the while loop
        if index == -1:
            break
        # print( "%s found at index %d" % (search, index) )
        # move to next possible start position
        lists.append(index)
        start = index + 1
    return lists#返回substring的位置列表






def replace_keyword(text,weapon):
    baseUrl = "http://192.168.0.100:8080/jsp/search.jsp?keyword=";
    url = "<a href='" + baseUrl + weapon + "'>" + weapon + "</a>";
    return text.replace(weapon,url)
    
    
def replace_keyword_reverse(text,weapon):
    baseUrl = "http://192.168.0.100:8080/jsp/search.jsp?keyword=";
    url = "<a href='" + baseUrl + weapon[::-1] + "'>" + weapon[::-1] + "</a>";
    return text.replace(weapon,url)




# -------------------------------------------------------------------------------------------
if __name__ == '__main__':
    path1='../../data/一个输入样例.txt'
    path2='../../data/mini实体表.txt'
    text=open_file(path1)#读取要被替换的文本
    keyword_list=read_keywords(path2)#读取需要替换的关键词

    keyword_list=list(set(keyword_list))#去重
    '''
    为什么需要去重呢？
    因为这里有两个EA-18G
    '''
    keyword_list.sort(reverse=True)#排序

    '''    
    这里为什么需要排序呢？
    是为了应对以下这种情况:
    
    EA-18G电子战机
    EA-18G“咆哮者”
    “咆哮者”
    EA-18G
    EA-1
    '''
# -------------------------------------------------------------------------------------------------

# 由于mini实体表.txt中的字符串包含关系都是先长后短。
# 因此判断的时候都是判断后者keyword_list[index+1]是否是前者item的substring

    longer_index=[]#表示keyword_list中第几个(从0开始算)keyword包含有substring
    for index,item in enumerate(keyword_list):
        print("item=",item)
        try:
            if keyword_list[index+1] in item:
                    longer_index.append(index)
            elif keyword_list[index+2] in item:
                    longer_index.append(index)
            else:
                pass
        except:
            pass 
    print("longer_index=",longer_index)
#     # -----------------------------------------------------------------------------------------------

#-------------------------------先替换"长度长"的关键词-----------------------------------------------------------------------
    longer_keywords=[]#与longer_index
    longer_keywords_reverse=[]
    for index in longer_index:
        text=text.replace(keyword_list[index],keyword_list[index][::-1])#这里故意替换成长关键词的逆序
        longer_keywords.append(keyword_list[index])
        longer_keywords_reverse.append(keyword_list[index][::-1])
    print("看下读取的文本内容",text)
    

    remnant_keyword=list(set(keyword_list) - set(longer_keywords))#得到剩下的关键词

    print("remnant_keyword=",remnant_keyword)
    print("longer_keywords=",longer_keywords)
    print("longer_keywords_reverse=",longer_keywords_reverse)
    
    
    
    
# 对剩余的关键词进行替换：
# remnant_keyword->url
    for index,item in enumerate(remnant_keyword):
        text=replace_keyword(text,item)
    
    for index,item in enumerate(longer_keywords_reverse):
        text=replace_keyword_reverse(text,item)
    print("text=",text)


    with open("result.txt", "w", encoding='utf-8') as f:
        f.write(text)
        f.close()


